/**
 * 
 */
package com.google.code.spt;

import java.util.HashMap;
import java.util.Map;

import com.google.code.spt.SparePart.PartColumns;
import com.google.code.spt.SparePart.TradeColumns;


import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.net.Uri;
import android.util.Log;

/**
 * @author Eason Lin
 * 
 */
public class SparePartProvider extends ContentProvider {

	/**
	 * @author Eason Lin
	 */
	/**
	 * A tag for log information
	 */
	private static final String TAG = "SparePartProvider";
	private static final String DATABASE_NAME = "sparepart.db";
    private static final int DATABASE_VERSION = 2;
    private static final String PARTS_TABLE_NAME = "parts";
    private static final String TRADE_TABLE_NAME = "trades";
    private static final int PARTS = 1;
    private static class DatabaseHelper extends SQLiteOpenHelper {

		/**
		 * This class help to open, create, and update the database file. 
		 * @param context
		 */
		public DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		/* (non-Javadoc)
		 * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
		 */
		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE " + PARTS_TABLE_NAME + " ("
					+ PartColumns._ID + " INTEGER PRIMARY KEY,"
					+ PartColumns.GUID + " CHAR(37),"
					+ PartColumns.NAME + " TEXT,"
					+ PartColumns.VIEW_ID + " INTEGER"
					+ ");");
			db.execSQL("CREATE TABLE " + TRADE_TABLE_NAME + " ("
					+ TradeColumns._ID + " INTEGER PRIMARY KEY,"
					+ TradeColumns.CREATED_DATE + " INTEGER,"
					+ TradeColumns.MODIFIED_DATE + " INTEGER,"
					+ TradeColumns.PART_ID + " INTEGER,"
					+ TradeColumns.PRICE + " INTEGER,"
					+ TradeColumns.TRADE_DATE + " INTEGER,"
					+ TradeColumns.DESCRIPTION + " TEXT"
					+ ");");

		}

		/* (non-Javadoc)
		 * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
		 */
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS parts");
			db.execSQL("DROP TABLE IF EXISTS trades");
			onCreate(db);
		}

	}

	private DatabaseHelper mOpenHelper;
	private HashMap<String, String> sPartsProjectionMap;
	private UriMatcher sUriMatcher;

	/**
	 * 
	 */
	public SparePartProvider() {
		// TODO Auto-generated constructor stub

	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.content.ContentProvider#delete(android.net.Uri,
	 * java.lang.String, java.lang.String[])
	 */
	@Override
	public int delete(Uri arg0, String arg1, String[] arg2) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.content.ContentProvider#getType(android.net.Uri)
	 */
	@Override
	public String getType(Uri arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.content.ContentProvider#insert(android.net.Uri,
	 * android.content.ContentValues)
	 */
	@Override
	public Uri insert(Uri arg0, ContentValues arg1) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.content.ContentProvider#onCreate()
	 */
	@Override
	public boolean onCreate() {
		mOpenHelper = new DatabaseHelper(getContext());
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.content.ContentProvider#query(android.net.Uri,
	 * java.lang.String[], java.lang.String, java.lang.String[],
	 * java.lang.String)
	 */
	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
			String sortOrder) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        qb.setTables(PARTS_TABLE_NAME);
        
        switch(sUriMatcher.match(uri)) {
        case PARTS:
        	qb.setProjectionMap(sPartsProjectionMap);
        	qb.appendWhere(PartColumns._ID + "=" + uri.getPathSegments().get(1));
        	break;
        	
        }
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.content.ContentProvider#update(android.net.Uri,
	 * android.content.ContentValues, java.lang.String, java.lang.String[])
	 */
	@Override
	public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
		// TODO Auto-generated method stub
		return 0;
	}

}
